package westh.ilib.struts.action.userTag;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import net.sf.json.JSONObject;

import org.apache.commons.logging.*;
import org.apache.struts.action.Action;
import org.apache.struts.action.ActionForm;
import org.apache.struts.action.ActionForward;
import org.apache.struts.action.ActionMapping;
import org.apache.struts.action.DynaActionForm;
import org.mule.extras.client.MuleClient;
import org.mule.umo.UMOMessage;

import westh.config.Config;
import westh.log.YyError;

/**
 * @author Administrator
 *
 */
public class AnnoRscAction extends Action {
	private final static Log log = LogFactory.getLog(AnnoRscAction.class);
	//get clouds Ip address
	String localhostUrl = Config.get("localhostUrl");
	String remoteUrl = Config.get("remoteUrl");
	
	public ActionForward execute(ActionMapping mapping,ActionForm form,HttpServletRequest request,HttpServletResponse response) {
		YyError.show("5502");
		DynaActionForm f = (DynaActionForm) form;
		String userId = f.getString("userId");
		String resourceId = f.getString("resourceId");
		String rating = f.getString("rating");
		String readStatus = f.getString("readStatus");
		String comments = f.getString("comments");
		String type = f.getString("type");
		String tagStr = f.getString("tagStr");
		
		JSONObject resultObject = new JSONObject();
		resultObject.put("userId", userId);
		resultObject.put("rscId", resourceId);
		resultObject.put("rscType", type);
		resultObject.put("score", rating);
		resultObject.put("readStatus", readStatus);
		resultObject.put("rscDes", comments);
		resultObject.put("tags", tagStr);
		String inputStr = resultObject.toString();
		
		//jSon-String
		String outString = "";
		try {
			MuleClient clientD = new MuleClient();
			// 调用WebService
			UMOMessage umo = clientD.send("axis:http://"+remoteUrl+":4481/DataCenter/TagDataCenter?method=saveTaggingJson",inputStr, null);
			outString = (String) umo.getPayload();
		} catch (Exception e) {
			throw new RuntimeException(e);
		}

		log.debug(outString);
		/**************获取jSon信息 end***************************/
		
		String retCode=""; //返回代码
		String retMsg="";//返回结果信息
		String returnStr="";//跳转信息
		/****************解析输出jSon串   start*********************/
		JSONObject jsonOut= JSONObject.fromObject(outString);
		retCode = jsonOut.getString("retCode");
		if (retCode.equals("0")) {
			retMsg = jsonOut.getString("retMsg");
			request.setAttribute("outString", retMsg);
			returnStr = "succ";
		}else{
			retMsg = jsonOut.getString("retMsg");
			request.setAttribute("info", retMsg);
			returnStr = "fail";
		}
		return mapping.findForward(returnStr);
		
	}

}
